<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品标签类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 * @version 1.0
 */

class goodsTag {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 保存和更新标签组
     *
     * @return unknown
     */
    public function saveTagGroupInfo ( $arg_array ) {
        //检查参数
        if (!isset($arg_array['tagGroupName'] ,$arg_array['sortOrder'] ,$arg_array['creator'], $arg_array['tagGroupID'], $arg_array['isColor']))
            return array( 'code' => 400, 'message' => '参数错误');

        $sortOrder 	 = $arg_array['sortOrder'] ;			//排序序号
        $tagGroupID = $arg_array['tagGroupID'] ;			//标签组号
        $isColor = $arg_array['isColor'];                   //是否为颜色标签属性
        $tagGroupName = $arg_array['tagGroupName'];       //标签组名字
        $useFlg = $arg_array['useFlg'];                     //启用状态
        $result = $this->dbca->stmt_query("SELECT * FROM goods_tag_group WHERE tagGroupName = ?","s",$tagGroupName);
        try{
            $tagGroupInfo = new entity('goods_tag_group') ;
            //如果标签组号不为空则查询检查标签组号
            if($tagGroupID != 0){
                //if( $tagGroupID == 1 || $tagGroupID == 2 )
                //throw new Exception('标签组「颜色,尺码」,不允许操作' , 400);

                $tagGroupInfo->tagGroupID = $tagGroupID ;
                if(!$this->dbca->fetch($tagGroupInfo)){
                    throw new Exception(sprintf('标签组号「%d」,的标签组不存在,请审核后再操作',$str_array[0]) , 400);
                }
                //检查标签组排序有没有被使用
                $issetSO = $this->dbca->stmt_query('SELECT COUNT(*) count FROM goods_tag_group WHERE sortOrder=? AND tagGroupID!=?','ii' ,$sortOrder, $tagGroupID) ;
                if($issetSO[0]->count > 0)
                    throw new Exception('排序序号已经被使用' , 400);

                $tagGroupInfo->editor = $arg_array['creator'] ;
            }else {
                //检查标签组排序有没有被使用
                $issetSO = $this->dbca->stmt_query('SELECT COUNT(*) count FROM goods_tag_group WHERE sortOrder=?','i' ,$sortOrder) ;
                if($issetSO[0]->count > 0){
                    throw new Exception('排序序号已经被使用' , 400);
                }
                if($result){
                    throw new Exception('标签组名称已经存在,请审核后再操作',400);
                }
                $tagGroupInfo->creator = $arg_array['creator'] ;
            }

            //保存和更新标签组信息
            $tagGroupInfo->tagGroupName = trim($arg_array['tagGroupName']) ;
            $tagGroupInfo->sortOrder 	  = $arg_array['sortOrder'] ;
            $tagGroupInfo->isColor       = $arg_array['isColor'];
            $tagGroupInfo->useFlg        = $arg_array['useFlg'];
            $this->dbca->save( $tagGroupInfo , 'goods' ) ;
            return array( 'code' => 200, 'message' => '标签组保存成功' ) ;
        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
    }

    /**
	 * 删除标签组
	 *
	 */
	public function deleteTagGroup ( $arg_array ) {
		
		//标签组号串检查
		if (!isset($arg_array['delete_str'])) {
            return array( 'code' => 400, 'message' => '缺少参数' );
		}    
            
		//将标签组号串截成数组
        $delete_array = explode('_', $arg_array['delete_str']);
        
        if (empty($delete_array)) {
            return array( 'code' => 400, 'message' => '请选择需要删除的商品标签' );
        }
        
        //检查当前标签是否存在商品
         foreach( $delete_array as &$c_l ) {
			$tag_group = new entity('goods_tag_group');
			$tag_group->tagGroupID = $c_l;
			if (!$this->dbca->fetch($tag_group)) {
				return array('code' => 400, 'message' => sprintf('商品标签号「%d」信息未找到，请核实', $c_l));
			}
			
			//是否存在商品
			$group_relation = $this->dbca->stmt_query('SELECT * FROM goods_tag_relation WHERE tagGroupID = ?','i',$c_l);	
			if ($group_relation) {
				return array('code' => 400, 'message' => sprintf('商品标签「%s」存在商品，不能删除', $tag_group->tagGroupName));
			}
         }
        
        try {
        	$this->dbca->beginTransaction();
        	
			//循环删除标签组
            foreach( $delete_array as &$g_l ) {

                //查询标签组信息
                $sql_del_taggroup_info = 'DELETE FROM goods_tag_group WHERE tagGroupID=?';
                
                $taggroup_del_result = $this->dbca->stmt_query($sql_del_taggroup_info,'i',$g_l);
                
				$sql_del_tag_info = 'DELETE FROM goods_tag WHERE tagGroupID=?';
				
                $tag_del_result = $this->dbca->stmt_query($sql_del_tag_info,'i',$g_l);
            }
            
            $this->dbca->commit();
            
            return array( 'code' => 200, 'message' => '商品标签删除成功' );
        } catch (Exception $e) {
        	$this->dbca->rollBack();
        	error_log($e->getTraceAsString());
            if ($e->getCode() > 0) 
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
	}

    /**
	 * 商品标签列表 
	 *
	 * @param unknown_type $arg_array 标签组参数
	 * @return unknown
	 */
	public function goodsTagList ( &$arg_array ) {
		//sql字符串
        $sql = '';
        
        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //标签名称
        if (isset($arg_array['tagName']) && trim($arg_array['tagName']) != '') {
            $sql .= ' AND sc.tagName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['tagName']) . '%';
        }

        //标签ID
        if (isset($arg_array['tagID']) && is_numeric($arg_array['tagID'])) {
            $sql .= ' AND sc.tagID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['tagID'];
        }

        //标签组号
        if (isset($arg_array['tagGroupID']) && trim($arg_array['tagGroupID']) != '') {
            $sql .= ' AND sc.tagGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['tagGroupID'];
        }
        
        //使用标志
    	if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
    		$sql .= ' AND sc.useFlg = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['useFlg'];
    	}
        
        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计促销行数
                $search_sql  = 'SELECT COUNT(*) AS count FROM goods_tag sc ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count  = $count_array[0]->count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY sc.%s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY sc.%s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询商品促销信息

            $search_sql = 'SELECT sc.tagID,sc.tagName,sc.memoName,sc.tagGroupID,sc.sortOrder,sc.useFlg,scg.tagGroupName FROM goods_tag sc 
						   INNER JOIN goods_tag_group scg ON sc.tagGroupID=scg.tagGroupID' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            
            if ($temp_item_array) {
				foreach($temp_item_array as &$item) {
				    $item_array[] = array(
				        'tagID' 	   => $item->tagID,
				        'tagName' 	   => $item->tagName,
				        'memoName' 	   => $item->memoName,
				        'tagGroupID'  => $item->tagGroupID,
				        'tagGroupName'  => $item->tagGroupName,
				        'tagGroupMemoName'  => $item->tagGroupMemoName,
				        'sortOrder'    => $item->sortOrder,
				        'useFlg' 	   => $item->useFlg
				    );
				}
            }
			
			return isset($item_array) ? $item_array : null;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
	}

    /**
     * 删除标签
     *
     */
    public function deleteTag ( $arg_array ) {
        //标签号串检查
        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的标签' );

        //将标签号串截成数组
        $delete_array = explode('_', $arg_array['delete_str']);

        try {
            //循环删除标签
            foreach( $delete_array as &$s_l ) {
                if (!is_numeric( $s_l ))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //查询标签信息
                $tagInfo = new entity('goods_tag');
                $tagInfo->TagID = $s_l;
                if ($this->dbca->fetch( $tagInfo ))
                    $this->dbca->delete( $tagInfo );
            }
            return array( 'code' => 200, 'message' => '删除成功' );
        } catch (Exception $e) {
            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );
        }
    }

    /**
     * 标签的使用和禁用
     *
     * @param unknown_type $arg_array
     * @return unknown
     */
    public function statusGoodsTag ( $arg_array ) {
        //检查参数
        if (!isset($arg_array['useFlg_str']) || $arg_array['useFlg_str'] == '')
            return array( 'code' => 400, 'message' => '缺少参数' );

        $str_array = explode('_', $arg_array['useFlg_str']);

        //查询标签是否存在或已经被操作
        $tagInfo = new entity('goods_tag');
        $tagInfo->tagID = $str_array[0] ;
        if (!$this->dbca->fetch( $tagInfo ))
            return array( 'code' => 400, 'message' => sprintf('标签号「%d」,的标签不存在,请审核后再操作',$str_array[0]));

        //检查标签状态是否已经修改
        if($tagInfo->useFlg == $str_array[1] && $str_array[1] == 1)
            return array( 'code' => 400, 'message' => sprintf('标签号「%d」,的标签已启用',$str_array[0]));

        if($tagInfo->useFlg == $str_array[1] && $str_array[1] == 0)
            return array( 'code' => 400, 'message' => sprintf('标签号「%d」,的标签已禁用',$str_array[0]));

        //修改标签状态
        $tagInfo->useFlg = $str_array[1] ;
        $this->dbca->save( $tagInfo , 'goods' );

        if($str_array[1] == 1)
            return array( 'code' => 200, 'message' => sprintf('标签号「%d」,的标签启用成功',$str_array[0]) );

        else if ($str_array[1] == 0)
            return array( 'code' => 200, 'message' => sprintf('标签号「%d」,的标签禁用成功',$str_array[0]) );
    }

    /**
     * 保存和更新标签
     *
     * @return unknown
     */
    public function saveGoodsTag ( $arg_array ) {

    	global $G_PGLOB;
    	 
		//检查参数
		if (!isset($arg_array['tagID'], $arg_array['tagName'], $arg_array['memoName'], $arg_array['sortOrder'], $arg_array['tag_array'])) {
			return array('code' => 400, 'message' => '缺少参数');
		}
		
		//检查标签名称是否重复
		$check_tag_group = new entity('goods_tag_group');
		$check_tag_group->tagGroupName = $arg_array['tagName'];
		if ($this->dbca->fetch($check_tag_group)) {
			if (!empty($arg_array['tagID'])) {
				if ($check_tag_group->tagGroupID != $arg_array['tagID']) {
					return array('code' => 400, 'message' => sprintf('商品标签「%s」已经存在', $arg_array['tagName']));
				}
			} else {
				return array('code' => 400, 'message' => sprintf('商品标签「%s」已经存在', $arg_array['tagName']));
			}
		}
		
		//检查排序序号是否重复
		$sort_tag_group = new entity('goods_tag_group');
		$sort_tag_group->sortOrder = $arg_array['sortOrder'];
		if ($this->dbca->fetch($sort_tag_group)) {
			if (!empty($arg_array['tagID'])) {
				if ($sort_tag_group->tagGroupID != $arg_array['tagID']) {
					return array('code' => 400, 'message' => sprintf('排序「%d」已经存在', $arg_array['sortOrder']));
				}
			} else {
				return array('code' => 400, 'message' => sprintf('排序「%d」已经存在', $arg_array['sortOrder']));
			}
		}
		
		$goods_tag_group = new entity('goods_tag_group');
		if (!empty($arg_array['tagID'])) {
			$goods_tag_group->tagGroupID = $arg_array['tagID'];
			if (!$this->dbca->fetch($goods_tag_group)) {
				return array('code' => 400, 'message' => '商品标签信息不存在');
			}	
			
			$goods_tag_group->editor = $arg_array['creator'];
			$alltagName = $this->dbca->stmt_query('SELECT tagName FROM goods_tag WHERE tagGroupID = ?','i',$arg_array['tagID']);
			
		} else {
			$goods_tag_group->creator = $arg_array['creator'];
		}
		$goods_tag_group->tagGroupName = $arg_array['tagName'];
		$goods_tag_group->memoName = $arg_array['memoName'];
		$goods_tag_group->sortOrder     = $arg_array['sortOrder'];
		$goods_tag_group->useFlg        = 1;
		
		$this->dbca->beginTransaction();
		try{
			$this->dbca->save($goods_tag_group);
			
			//获取标签ID
			if (!empty($arg_array['tagID'])) {
				$tagGroupID = $arg_array['tagID'];
			} else {
				$tagGroup = $this->dbca->stmt_query('SELECT MAX(tagGroupID) max_id FROM goods_tag_group');
				$tagGroupID = $tagGroup[0]->max_id;
			}
			
			if (isset($alltagName) && !empty($alltagName)) {

                foreach ($alltagName as $v) {
					$tagName = $v->tagName;
					$tagFlg = 0;
					foreach ($arg_array['tag_array'] as $k=>$t) {
						if ($tagName == $t[0]) {
							$tagFlg = 1;
							$goods_tag = new entity('goods_tag');
							$goods_tag->tagGroupID = $arg_array['tagID'];
							$goods_tag->tagName = $tagName;
							if (!$this->dbca->fetch($goods_tag)) {
								throw new Exception('查不到规格信息',404);
							}
							$goods_tag->editor = $arg_array['creator'];
							$goods_tag->memoName = $t[1];
							$this->dbca->save($goods_tag);
							unset($arg_array['tag_array'][$k]);
						} else {
							if ($tagFlg == 0 && count($arg_array['tag_array']) == $k+1) {
								$this->dbca->stmt_query('DELETE FROM goods_tag WHERE tagGroupID = ? AND tagName = ?','is',array($arg_array['tagID'],$tagName));
							}
						}
					}
				}
            }
			
            //获取标签值最大排序
			$max_sort = $this->dbca->stmt_query('SELECT MAX(sortOrder)+1 max_sort FROM goods_tag');
			if (empty($max_sort[0]->max_sort)) {
				$max_sort = 1;
			} else {
				$max_sort = $max_sort[0]->max_sort;
			}
			
			if (!empty($arg_array['tag_array'])) {
				foreach ($arg_array['tag_array'] as $tag) {
					$goods_tag = new entity('goods_tag');
	                $goods_tag->creator     = $arg_array['creator'];
					$goods_tag->tagName    = $tag[0];
					$goods_tag->memoName    = $tag[1];
					$goods_tag->tagGroupID = $tagGroupID;
					$goods_tag->tagGroupName = $arg_array['tagName'];
					$goods_tag->tagGroupMemoName = $arg_array['memoName'];
					$goods_tag->sortOrder   = $max_sort;
					$goods_tag->useFlg      = 1;
	                $this->dbca->save($goods_tag);  
	                $max_sort++;
				}     
			}
			
			$this->dbca->commit();
			if (!empty($arg_array['tagID'])) {
				return array('code' => 200, 'message' => '商品标签更新成功', 'tagID' => $arg_array['tagID'], 'sortOrder' => $arg_array['sortOrder']+1);
			} else {
				return array('code' => 200, 'message' => '商品标签保存成功', 'tagID' => '', 'sortOrder' => $arg_array['sortOrder']+1);
			}
		
		} catch(Exception $e) {
			
			error_log($e->getMessage());
			
			$this->dbca->rollBack();
			
			return array('code' => 400, 'message' => '商品标签保存失败');
		}
	}
    
    /**
     * 查询标签组列表
     *
     * @param unknown_type $arg_array
     * @return unknown
     */
	public function searchGoodsTagList(&$arg_array){
		
	    //sql字符串
	    $sql = '';
	
	    //参数类型字符串
	    $params_type = '';
	
	    //参数值数组
	    $params_array = array();
	
	    //标签组ID
	    if (isset($arg_array['tagGroupID']) && is_numeric($arg_array['tagGroupID'])) {
	        $sql .= ' AND tagGroupID = ?';
	        $params_type .= 'i';
	        $params_array[] = $arg_array['tagGroupID'];
	    }
	
	    //标签组名称
	    if (isset($arg_array['tagGroupName']) && !empty($arg_array['tagGroupName'])) {
	        $sql .= ' AND tagGroupName LIKE ?';
	        $params_type .= 's';
	        $params_array[] = '%' . $arg_array['tagGroupName'] . '%';
	    }
	
	    //使用状态 0:禁用 1:启用
	    if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0 ,1))) {
	        $sql .= ' AND useFlg = ?';
	        $params_type .= 'i';
	        $params_array[] = $arg_array['useFlg'];
	    }
	
	    //扩展查询
	    if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
	        $sql .= $arg_array['extend'];
	    }
	
	    if ($sql) {
	        $sql = 'WHERE ' . substr($sql, 4);
	    }
	
	    try {
	
	        if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
	
	            //统计分类数量
	            $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_tag_group ' . $sql;
	            $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
	            $item_count = $count_array[0]->item_count;
	            if ($item_count == 0) {
	                $arg_array['count'] = 0;
	                return null;
	            }
	
	            $arg_array['count'] = $item_count;
	
	            //排序
	            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
	                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
	
	            //分页
	            if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
	                $arg_array['page'] = 1;
	
	            $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
	
	        } else {
	
	            $arg_array['count'] = 0;
	
	            //排序
	            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
	                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
	
	        }
	
	        //查询信息
	        $search_sql = 'SELECT * FROM goods_tag_group ' . $sql;
	
	        $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
	        if (!$temp_item_array)
	            return null;
	
	        foreach($temp_item_array as &$item) {
	            $item_array[] = array(
	                'createTime' => $item->createTime,
	                'creator' => $item->creator,
	                'tagGroupID' => $item->tagGroupID,
	                'tagGroupName' => $item->tagGroupName,
	                'sortOrder' => $item->sortOrder,
	                'useFlg' => $item->useFlg,
	                'isColor' => $item->isColor
	            );
	        }
	
	        return $item_array;
	
	    } catch (Exception $e) {
	
	        error_log($e->getMessage());
	        return null;
	
	    }
	
	}
	
	/**
	 * 标签最大序号
	 *
	 */
	public function maxSortOrder(){
		$sortOrder = &$this->dbca->stmt_query('SELECT MAX(sortOrder) sort FROM goods_tag_group');
		if (is_null($sortOrder[0]->sort)) {
			return 1;
		} else {
			return $sortOrder[0]->sort + 1;
		}
	}
	
	/**
	 * 商品标签组列表 
	 *
	 * @param unknown_type $arg_array 标签组参数
	 * @return unknown
	 */
	public function goodsTagGroupList ( &$arg_array ) {
	
		 //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //标签组号
        if (isset($arg_array['tagGroupID']) && is_numeric($arg_array['tagGroupID'])) {
            $sql .= ' AND tagGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['tagGroupID'];
        }

        //标签组名称
        if (isset($arg_array['tagGroupName']) && !empty($arg_array['tagGroupName'])) {
            $sql .= ' AND tagGroupName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['tagGroupName']) . '%';
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_tag_group ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_tag_group ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
            	
				$tagVal = '';
            	
        		$tagArray = array(
        			'tagGroupID' => $item->tagGroupID,
        			'sortorder' => 'DESC',
        			'sortname' => 'sortOrder'
        		);
        		$tagData = $this->goodsTagList($tagArray);
        		if ($tagData) {
        			foreach ($tagData as $key=>$tag){
        				if (count($tagData) == $key+1) {
        					$tagVal .= $tag['tagName'];
        				} else {
        					$tagVal .= $tag['tagName'] . '，';
        				}
        			}
            	}
            	
                $item_array[] = array(
                    'tagGroupID' 	=> $item->tagGroupID,
                    'tagGroupName' => $item->tagGroupName,
                    'memoName' => $item->memoName,
                    'sortOrder' 	=> $item->sortOrder,
                    'useFlg' 		=> $item->useFlg,
                    'tagVal'	    => $tagVal,
                    'tagArray'		=> $tagData
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
	}
	
	/**
     * 标签组的使用和禁用
     *
     * @param unknown_type $arg_array
     * @return unknown
     */

    public function changeUse($arg_array){

        if (!isset($arg_array['tagGroupID']) || $arg_array['tagGroupID'] == '')
            return array( 'code' => 400, 'message' => '缺少参数' );

        //查询标签组是否存在或已经被操作
        $tagGroupInfo = new entity('goods_tag_group');
        $tagGroupInfo->tagGroupID = $arg_array['tagGroupID'] ;
        if (!$this->dbca->fetch( $tagGroupInfo ))
            return array( 'code' => 400, 'message' => sprintf('商品标签号「%d」信息未找到，请核实',$arg_array['tagGroupID'] ));

        //检查标签组状态是否已经修改
        if($tagGroupInfo->useFlg == $arg_array['useFlg']  && $arg_array['useFlg'] == 1)
            return array( 'code' => 400, 'message' => sprintf('商品标签「%s」已启用',$tagGroupInfo->tagGroupName ));

        if($tagGroupInfo->useFlg == $arg_array['useFlg'] && $arg_array['useFlg'] == 0)
            return array( 'code' => 400, 'message' => sprintf('商品标签「%s」已禁用',$tagGroupInfo->tagGroupName ));

        //修改标签组状态
        $tagGroupInfo->useFlg = $arg_array['useFlg'] ;
        $this->dbca->save( $tagGroupInfo , 'goods_tag_group' );

        if($arg_array['useFlg'] == 1)
            return array( 'code' => 200, 'message' => sprintf('商品标签「%s」启用成功',$tagGroupInfo->tagGroupName ) );

        else if ($arg_array['useFlg'] == 0)
            return array( 'code' => 200, 'message' => sprintf('商品标签「%s」禁用成功',$tagGroupInfo->tagGroupName ) );
    }
	
	/**
     * 查询商品标签映射
     */
    public function searchTagRelation(&$arg_array) {
    
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//网购号
    	if (isset($arg_array['postID']) && is_numeric($arg_array['postID'])) {
    		$sql .= ' AND postID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['postID'];
    	}
    
    	if ($sql) {
    		$sql = ' WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计促销行数
    			$search_sql  = 'SELECT COUNT(*) AS count FROM goods_tag_relation ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count  = $count_array[0]->count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    		}
    
    		//查询商品促销信息
    
    		$search_sql = 'SELECT * FROM goods_tag_relation' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    
    		if ($temp_item_array) {
    			foreach($temp_item_array as &$item) {

                    $tag_args_array = array(
                        'tagID' => $item->tagID
                    );
                    $goods_tag = $this->goodsTagList( $tag_args_array );
                    if (!$goods_tag) {
                        continue;
                    }

    				$item_array[] = array(
						'postID' 	    => $item->postID,
						'tagGroupID' 	=> $item->tagGroupID,
						'tagGroupName' 	=> $item->tagGroupName,
						'tagID' 	    => $item->tagID,
						'tagName'        => $item->tagName,
						'memoName'        => $goods_tag[0]['memoName']
    				);
    			}
    		}
    
    		return isset($item_array) ? $item_array : null;
    
    	} catch (Exception $e) {
    
    		error_log($e->getMessage());
    		return null;
    
    	}
    }
}